package org.brisling.oa.service.base;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.inf.BaseFlowDomainInf;
import org.brisling.base.service.BaseFlowService;
import org.brisling.common.ConstSysParam;
import org.brisling.docManage.domain.Docdetail;
import org.brisling.oa.domain.BaseOADomain;
import org.brisling.oa.domain.BaseOADomainInf;
import org.brisling.oa.domain.OADeptPayment;
import org.brisling.sysman.service.UploadService;
import org.brisling.workflow.queryService.QueryTaskconfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 * <p>
 * Title: BaseOaFlowService
 * </p>
 * <p>
 * Description: oa系统模块服务基类
 * </p>
 * <p>
 * Company: 长沙东勤信息科技发展有限公司
 * </p>
 * 
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2017年7月25日 上午11:01:41
 */
public class BaseOaFlowService<T extends BaseFlowDomainInf> extends BaseFlowService<T> {

	@PersistenceContext(unitName = ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
		this.subSystem = ConstSysParam.SUBSYSTEM_OA;
	}

	@Autowired
	private QueryTaskconfigService qryTaskService;

	// 登录用户验证及用户信息服务类
	@Autowired
	protected ShiroSecurityService shiroSecurityService;

	@Autowired
	private UploadService uploadService;

	/**
	 * 保存实体
	 * 
	 * @param t
	 * @return
	 */

	@SuppressWarnings("rawtypes")
	protected T setFieldValue(T t, T old) {

		HashMap taskConfig = null;

		if (t != null) {

			// 判断是否update操作
			if (old != null) {
				// 获取业务可填、必填项
				Map<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("fullBizcode", t.getFullBizcode());
				paramMap.put("activityId", t.getTask_def_key());
				Object obj = qryTaskService.getTaskConfig(paramMap);

				if (obj != null) {
					taskConfig = (HashMap) obj;
				}

				// 判断必填字段
				if (taskConfig.containsKey(ConstSysParam.REQUIRED_FIELD)) {
					Object ro = taskConfig.get(ConstSysParam.REQUIRED_FIELD);
					if (ro != null) {
						String rf = ro.toString();
						String[] _field = rf.split(",");
						for (String _f : _field) {
							BeanUtils.copyProperties(t, old, _f);
						}

					}
				}

				// 判断可填字段
				if (taskConfig.containsKey(ConstSysParam.EDITABLE_FIELD)) {
					Object ro = taskConfig.get(ConstSysParam.EDITABLE_FIELD);
					if (ro != null) {
						String rf = ro.toString();
						String[] _field = rf.split(",");
						for (String _f : _field) {
							BeanUtils.copyProperties(t, old, _f);
						}
					}
				}

				if (old.getFlowTaskId() != null) {
					t.setFlowTaskId(old.getFlowTaskId());
				}
				// 从原有数据中复制可填、必填项到待保存记录中
			}
		} else {
			return null;
		}
		return t;
	}

	/**
	 * 保存业务数据
	 * 
	 * @param body
	 *            业务数据参数
	 * @return
	 */
	protected T saveOA(@RequestBody String body) {
		T beforeSavedDomain = bodyToObj(body, clazz);
		if (beforeSavedDomain != null) {
			if (beforeSavedDomain.getBizno() == null || beforeSavedDomain.getBizno().trim().length() <= 0) {
				beforeSavedDomain.setBizno(super.generateBizno());
			}
		}

		// 执行保存
		T savedDomain = null;

		// 根据id值判断是否新增操作
		if (beforeSavedDomain.getId() == null) {
			savedDomain = this.saveFlowEntity(shiroSecurityService.getCurrentUserid().toString(), beforeSavedDomain);
			// 更新附件，将临时id(tmpid)替换为保存后的id值
			uploadService.updateTmpid(beforeSavedDomain.getBizcode(), "'" + beforeSavedDomain.getTmpid() + "'",
					savedDomain.getId().toString());

		} else {
			savedDomain = this.saveFlowEntity(shiroSecurityService.getCurrentUserid().toString(), beforeSavedDomain);
		}

		return savedDomain;
	}

	/**
	 * 
	 * @param userid
	 * @param domain
	 * @param signInfo
	 * @return
	 */
	protected String completeTaskAndCandidateByEntity(String userid, BaseOADomainInf domain, String signInfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userid", domain.getApply_id());
		map.put("deptid", domain.getApply_deptid());
		return super.completeTaskAndCandidateByEntity(userid, domain, signInfo, map);
	}
}
